package net.dgg.zz.credential.generalCredential.service;

import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.common.service.CommonService;
import net.dgg.zz.common.utils.DateUtils;
import net.dgg.zz.credential.generalCredential.dao.CertificateStatisticDao;
import net.dgg.zz.credential.warningConfigure.dao.WarningConfigureDao;
import net.dgg.zz.credential.warningConfigure.entity.WarningConfigure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zsx
 * @Title: CertificateStatisticsService
 * @ProjectName zz-sc-zs
 * @Description: 证书总览统计
 * @date 2018/9/5 9:32
 */
@Service
public class CertificateStatisticService {
    @Autowired
    private CommonService commonService;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private WarningConfigureDao configureDao;
    @Autowired
    private CertificateStatisticDao statisticDao;

    /*
     * @Author zsx
     * @Date 2018-09-05 10:09:15
     * @Description 获取超期预警
     * @param signOrgId 事业部id
     * @param isAll 是否有权查询所有 （0表示只能看自己事业部）
     * @return Map<String, Object>
     */
    public Map<String, Object> findExpirationWarning(Long signOrgId, String isAll) {
        if (isAll.equals("0")) {
            signOrgId = commonService.findUserSyb(sessionManager.getCurrentUser());
        }
        Map<String, Object> params = new HashMap();
        params.put("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        params.put("generalStatus", CredentialConstant.GENERAL_CREDENTIAL_STATUS_NO_USE);
        params.put("libraryStatus", CredentialConstant.GENERAL_CREDENTIAL_LIBRARY_STATUS_IN);
        //查询单个事业部
        if (null != signOrgId) {
            params.put("signOrgId", signOrgId);
            WarningConfigure configure = configureDao.selectBySignOrgIdAndWarningType(signOrgId, CredentialConstant.GENERAL_CREDENTIAL);
            if (null == configure) {
                params.put("expiresTime", new Date());
                params.put("attachedTime", new Date());
                params.put("talentContractTime", new Date());
                params.put("unusedTime", new Date());

            }else {
                //证书到期预警量
                Date expiresTime = DateUtils.getFewDaysAgo(null == configure.getCertificateExpireWarning() ? 0 : configure.getCertificateExpireWarning().intValue());
                params.put("expiresTime", expiresTime);
                //证书使用预警量
                Date attachedTime = DateUtils.getFewDaysAgo(null == configure.getCertificateHangUpWarning() ? 0 : configure.getCertificateHangUpWarning().intValue());
                params.put("attachedTime", attachedTime);
                //人才合同到期预警量
                Date talentContractTime = DateUtils.getFewDaysAgo(null == configure.getTalentContractWarning() ? 0 : configure.getTalentContractWarning().intValue());
                params.put("talentContractTime", talentContractTime);
                //企业合同到期预警量
                Date companyContractTime = DateUtils.getFewDaysAgo(null == configure.getCompanyContractWarning() ? 0 : configure.getCompanyContractWarning().intValue());
                params.put("companyContractTime", companyContractTime);
                //证书未使用预警量
                Date unusedTime = DateUtils.getFewDaysAgo(null == configure.getCertificateNotUsedWarning() ? 0 : -(configure.getCertificateNotUsedWarning().intValue()));
                params.put("unusedTime", unusedTime);
            }
            return statisticDao.selectWarningAmountBysignOrgId(params);
        } else {//查询所有
            return statisticDao.selectAllWarningAmount(params);
        }
    }


}
